using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using TKT.Lifter.CIM.Lifter.Global;
using TKT.Lifter.CIM.Lifter.Gui.Monitor.Popups;

namespace Lifter.Gui.Monitor.Panels
{
	public class InformError : InformLogMenu
	{
		private delegate void LogReadDelegate();
		private delegate void AddDataGrid(string readString);
		private Thread threadSearch = null;
		private IContainer components = null;
		private Panel panelProgress;
		private Label labelMessage;
		private UIPanel panelSearch;
		private UIMarkLabel uiMarkLabel1;
		private UISymbolButton buttonSearch;
		private UIPanel uiPanel;
        private UIDatePicker selectDay;
        private UIMarkLabel uiMarkLabel3;
        private UIComboBox ToDate;
        private UIComboBox fromDate;
        private UIMarkLabel lbStartTime;
        private UITextBox tbErrorLog;
        private string saveFilePath = "";
		public InformError()
		{
			this.InitializeComponent();
			this.selectDay.Value = DateTime.Now;
		}
		private void buttonSearch_Click(object sender, EventArgs e)
		{
			if (this.threadSearch == null)
			{
				this.threadSearch = new Thread(new ThreadStart(this.ErrorLogRead));
			}
			if (this.threadSearch.ThreadState != ThreadState.Running)
			{
				if (this.threadSearch.ThreadState == ThreadState.Stopped)
				{
					this.threadSearch = new Thread(new ThreadStart(this.ErrorLogRead));
					this.threadSearch.Start();
				}
				else if (this.threadSearch.ThreadState == ThreadState.Stopped)
				{
					this.threadSearch.Start();
				}
				else if (this.threadSearch.ThreadState == ThreadState.Unstarted)
				{
					this.threadSearch.Start();
				}
			}
		}
		private void StartSearch()
		{
			this.panelProgress.BringToFront();
			this.labelMessage.Text = "正在查询数据。请稍等。";
			this.tbErrorLog.Clear();
			this.panelSearch.Enabled = false;
		}
		private void EndSearch()
		{
			this.panelSearch.Enabled = true;
			this.panelProgress.SendToBack();
		}
		private void ErrorLogRead()
		{
            try
            {
				base.BeginInvoke(new InformError.LogReadDelegate(this.StartSearch));
                string path = AppDomain.CurrentDomain.BaseDirectory + "Log\\LogError";

                List<FileInfo> filesForReading = this.GetFilesForReading(path);
                foreach (FileInfo current in filesForReading)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    using (StreamReader streamReader = new StreamReader(new FileStream(current.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.GetEncoding("GB2312")))
                    {
                        if (streamReader == null || current.Length == 0L)
                        {
                            continue;
                        }
                        stringBuilder = new StringBuilder(streamReader.ReadToEnd());
                        base.BeginInvoke(new InformError.LogReadDelegate(() => { this.tbErrorLog.AppendText(stringBuilder.ToString()); }));
                    }
                }
                base.BeginInvoke(new InformError.LogReadDelegate(this.EndSearch));
			}
            catch (Exception e)
            {
				PopupNotice.DialogPopups(e.Message, e.Message, enumPopupsType.Error);
				base.BeginInvoke(new InformError.LogReadDelegate(this.EndSearch));
			}
		}
		private List<FileInfo> GetFilesForReading(string path)
		{
            DateTimeConverter dateTimeConverter = new DateTimeConverter();
            DateTime selectDate = (DateTime)dateTimeConverter.ConvertFromString(this.selectDay.Text);
            string fileName = selectDate.ToString("yyyyMMdd");

            FileInfo[] files = new DirectoryInfo(path).GetFiles(fileName + "-*.txt");
            List<FileInfo> list = new List<FileInfo>();
            int startNum = string.IsNullOrEmpty(this.fromDate.Text) ? 0 : Convert.ToInt32(this.fromDate.Text);
            int toNum = string.IsNullOrEmpty(this.ToDate.Text) ? 24 : Convert.ToInt32(this.ToDate.Text);
            foreach (FileInfo item in files)
            {
                string num = item.Name.Substring(9, 2);
                int fileNum = Convert.ToInt32(num);

                if (startNum <= fileNum && fileNum <= toNum)
                {
                    list.Add(item);
                }
            }
            return list;
        }
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
            this.panelProgress = new System.Windows.Forms.Panel();
            this.labelMessage = new System.Windows.Forms.Label();
            this.panelSearch = new Sunny.UI.UIPanel();
            this.lbStartTime = new Sunny.UI.UIMarkLabel();
            this.uiMarkLabel3 = new Sunny.UI.UIMarkLabel();
            this.ToDate = new Sunny.UI.UIComboBox();
            this.selectDay = new Sunny.UI.UIDatePicker();
            this.fromDate = new Sunny.UI.UIComboBox();
            this.uiMarkLabel1 = new Sunny.UI.UIMarkLabel();
            this.buttonSearch = new Sunny.UI.UISymbolButton();
            this.uiPanel = new Sunny.UI.UIPanel();
            this.tbErrorLog = new Sunny.UI.UITextBox();
            this.panelProgress.SuspendLayout();
            this.panelSearch.SuspendLayout();
            this.uiPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // panelProgress
            // 
            this.panelProgress.BackColor = System.Drawing.SystemColors.ControlLight;
            this.panelProgress.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.panelProgress.Controls.Add(this.labelMessage);
            this.panelProgress.Location = new System.Drawing.Point(218, 166);
            this.panelProgress.Name = "panelProgress";
            this.panelProgress.Padding = new System.Windows.Forms.Padding(2);
            this.panelProgress.Size = new System.Drawing.Size(509, 55);
            this.panelProgress.TabIndex = 15;
            // 
            // labelMessage
            // 
            this.labelMessage.BackColor = System.Drawing.Color.RoyalBlue;
            this.labelMessage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.labelMessage.Dock = System.Windows.Forms.DockStyle.Fill;
            this.labelMessage.Font = new System.Drawing.Font("Arial", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.labelMessage.ForeColor = System.Drawing.Color.MistyRose;
            this.labelMessage.Location = new System.Drawing.Point(2, 2);
            this.labelMessage.Name = "labelMessage";
            this.labelMessage.Size = new System.Drawing.Size(503, 49);
            this.labelMessage.TabIndex = 0;
            this.labelMessage.Text = "正在查看日志信息。请稍等";
            this.labelMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // panelSearch
            // 
            this.panelSearch.Controls.Add(this.lbStartTime);
            this.panelSearch.Controls.Add(this.uiMarkLabel3);
            this.panelSearch.Controls.Add(this.ToDate);
            this.panelSearch.Controls.Add(this.selectDay);
            this.panelSearch.Controls.Add(this.fromDate);
            this.panelSearch.Controls.Add(this.uiMarkLabel1);
            this.panelSearch.Controls.Add(this.buttonSearch);
            this.panelSearch.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panelSearch.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.panelSearch.Location = new System.Drawing.Point(0, 576);
            this.panelSearch.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.panelSearch.MinimumSize = new System.Drawing.Size(1, 1);
            this.panelSearch.Name = "panelSearch";
            this.panelSearch.Radius = 1;
            this.panelSearch.Size = new System.Drawing.Size(983, 40);
            this.panelSearch.TabIndex = 22;
            this.panelSearch.Text = null;
            this.panelSearch.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
            this.panelSearch.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
            // 
            // lbStartTime
            // 
            this.lbStartTime.AutoSize = true;
            this.lbStartTime.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lbStartTime.Location = new System.Drawing.Point(235, 9);
            this.lbStartTime.Name = "lbStartTime";
            this.lbStartTime.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
            this.lbStartTime.Size = new System.Drawing.Size(79, 22);
            this.lbStartTime.TabIndex = 97;
            this.lbStartTime.Text = "开始小时";
            this.lbStartTime.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.lbStartTime.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
            // 
            // uiMarkLabel3
            // 
            this.uiMarkLabel3.AutoSize = true;
            this.uiMarkLabel3.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiMarkLabel3.Location = new System.Drawing.Point(404, 9);
            this.uiMarkLabel3.Name = "uiMarkLabel3";
            this.uiMarkLabel3.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
            this.uiMarkLabel3.Size = new System.Drawing.Size(79, 22);
            this.uiMarkLabel3.TabIndex = 96;
            this.uiMarkLabel3.Text = "结束小时";
            this.uiMarkLabel3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.uiMarkLabel3.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
            // 
            // ToDate
            // 
            this.ToDate.DataSource = null;
            this.ToDate.FillColor = System.Drawing.Color.White;
            this.ToDate.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.ToDate.Items.AddRange(new object[] {
            "00",
            "01",
            "02",
            "03",
            "04",
            "05",
            "06",
            "07",
            "08",
            "09",
            "10",
            "12",
            "13",
            "14",
            "15",
            "16",
            "17",
            "18",
            "19",
            "20",
            "21",
            "22",
            "23",
            "24"});
            this.ToDate.Location = new System.Drawing.Point(490, 6);
            this.ToDate.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.ToDate.MinimumSize = new System.Drawing.Size(63, 0);
            this.ToDate.Name = "ToDate";
            this.ToDate.Padding = new System.Windows.Forms.Padding(0, 0, 30, 2);
            this.ToDate.Size = new System.Drawing.Size(77, 29);
            this.ToDate.TabIndex = 95;
            this.ToDate.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.ToDate.Watermark = "";
            this.ToDate.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
            // 
            // selectDay
            // 
            this.selectDay.FillColor = System.Drawing.Color.White;
            this.selectDay.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.selectDay.Location = new System.Drawing.Point(81, 4);
            this.selectDay.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.selectDay.MaxLength = 10;
            this.selectDay.MinimumSize = new System.Drawing.Size(63, 0);
            this.selectDay.Name = "selectDay";
            this.selectDay.Padding = new System.Windows.Forms.Padding(0, 0, 30, 2);
            this.selectDay.Size = new System.Drawing.Size(140, 32);
            this.selectDay.SymbolDropDown = 61555;
            this.selectDay.SymbolNormal = 61555;
            this.selectDay.TabIndex = 89;
            this.selectDay.Text = "2024-03-25";
            this.selectDay.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.selectDay.Value = new System.DateTime(2024, 3, 25, 15, 6, 10, 444);
            this.selectDay.Watermark = "";
            this.selectDay.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
            // 
            // fromDate
            // 
            this.fromDate.DataSource = null;
            this.fromDate.FillColor = System.Drawing.Color.White;
            this.fromDate.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.fromDate.Items.AddRange(new object[] {
            "00",
            "01",
            "02",
            "03",
            "04",
            "05",
            "06",
            "07",
            "08",
            "09",
            "10",
            "12",
            "13",
            "14",
            "15",
            "16",
            "17",
            "18",
            "19",
            "20",
            "21",
            "22",
            "23",
            "24"});
            this.fromDate.Location = new System.Drawing.Point(321, 5);
            this.fromDate.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.fromDate.MinimumSize = new System.Drawing.Size(63, 0);
            this.fromDate.Name = "fromDate";
            this.fromDate.Padding = new System.Windows.Forms.Padding(0, 0, 30, 2);
            this.fromDate.Size = new System.Drawing.Size(77, 29);
            this.fromDate.TabIndex = 94;
            this.fromDate.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.fromDate.Watermark = "";
            this.fromDate.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
            // 
            // uiMarkLabel1
            // 
            this.uiMarkLabel1.AutoSize = true;
            this.uiMarkLabel1.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiMarkLabel1.Location = new System.Drawing.Point(3, 7);
            this.uiMarkLabel1.Name = "uiMarkLabel1";
            this.uiMarkLabel1.Padding = new System.Windows.Forms.Padding(5, 0, 0, 0);
            this.uiMarkLabel1.Size = new System.Drawing.Size(79, 22);
            this.uiMarkLabel1.TabIndex = 87;
            this.uiMarkLabel1.Text = "查询时间";
            this.uiMarkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.uiMarkLabel1.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
            // 
            // buttonSearch
            // 
            this.buttonSearch.Cursor = System.Windows.Forms.Cursors.Hand;
            this.buttonSearch.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.buttonSearch.Location = new System.Drawing.Point(574, 5);
            this.buttonSearch.MinimumSize = new System.Drawing.Size(1, 1);
            this.buttonSearch.Name = "buttonSearch";
            this.buttonSearch.Size = new System.Drawing.Size(100, 32);
            this.buttonSearch.Style = Sunny.UI.UIStyle.Custom;
            this.buttonSearch.StyleCustomMode = true;
            this.buttonSearch.Symbol = 361442;
            this.buttonSearch.TabIndex = 82;
            this.buttonSearch.Text = "查询";
            this.buttonSearch.TipsFont = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.buttonSearch.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
            this.buttonSearch.Click += new System.EventHandler(this.buttonSearch_Click);
            // 
            // uiPanel
            // 
            this.uiPanel.Controls.Add(this.tbErrorLog);
            this.uiPanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.uiPanel.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiPanel.Location = new System.Drawing.Point(0, 0);
            this.uiPanel.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.uiPanel.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiPanel.Name = "uiPanel";
            this.uiPanel.Size = new System.Drawing.Size(983, 576);
            this.uiPanel.TabIndex = 23;
            this.uiPanel.Text = "uiPanel2";
            this.uiPanel.TextAlignment = System.Drawing.ContentAlignment.MiddleCenter;
            this.uiPanel.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
            // 
            // tbErrorLog
            // 
            this.tbErrorLog.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.tbErrorLog.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tbErrorLog.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.tbErrorLog.Location = new System.Drawing.Point(0, 0);
            this.tbErrorLog.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.tbErrorLog.MinimumSize = new System.Drawing.Size(1, 16);
            this.tbErrorLog.Multiline = true;
            this.tbErrorLog.Name = "tbErrorLog";
            this.tbErrorLog.ShowText = false;
            this.tbErrorLog.Size = new System.Drawing.Size(983, 576);
            this.tbErrorLog.TabIndex = 1;
            this.tbErrorLog.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.tbErrorLog.Watermark = "";
            this.tbErrorLog.ZoomScaleRect = new System.Drawing.Rectangle(0, 0, 0, 0);
            // 
            // InformError
            // 
            this.Controls.Add(this.uiPanel);
            this.Controls.Add(this.panelSearch);
            this.Controls.Add(this.panelProgress);
            this.Name = "InformError";
            this.Size = new System.Drawing.Size(983, 616);
            this.panelProgress.ResumeLayout(false);
            this.panelSearch.ResumeLayout(false);
            this.panelSearch.PerformLayout();
            this.uiPanel.ResumeLayout(false);
            this.ResumeLayout(false);

		}
    }
}
